package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	conn, err := net.Dial("udp", "127.0.0.1:8003")
	if err != nil {
		fmt.Println("dial error", err)
		return
	}
	fmt.Println("连接服务端成功...")
	defer conn.Close()

	go func() {
		for {
			buf := make([]byte, 4096)
			n, err := os.Stdin.Read(buf)
			if (err != nil) {
				fmt.Println("read error", err)
				continue
			}
			conn.Write(buf[:n])
		}
	}()

	buf1 := make([]byte, 4096)
	for {
		n, err := conn.Read(buf1)
		if n == 0 {
			fmt.Println("服务端已关闭，关闭客户端!")
			return
		}
		if err != nil {
			fmt.Println("read error1", err)
			return
		}
		fmt.Println("客户端读到服务端回发数据：", string(buf1[:n]))
	}
}
